package com.szy.bigevent.controller;

import com.github.pagehelper.PageInfo;
import com.szy.bigevent.pojo.Article;
import com.szy.bigevent.pojo.Result;
import com.szy.bigevent.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/findAll")
    public Result<PageInfo<Article>> findAll(int pageNum, int pageSize,Article article) {
        PageInfo<Article> pageInfo = articleService.selectAll(pageSize, pageNum,article);
        return Result.success(pageInfo);
    }

    @DeleteMapping
    public Result delete(int id) {
        articleService.deleteById(id);
        return Result.success(null,"删除文章成功");
    }

    @PostMapping
    public Result add(@RequestBody Article article) {

        int cnt = articleService.insertSelective(article);
        if (cnt > 0) {
            return Result.success(null,"添加成功");
        }
        return Result.error("添加失败");
    }

    @PutMapping
    public Result update(@RequestBody Article article) {

        int cnt = articleService.updateByIdSelective(article);
        if (cnt > 0) {
            return Result.success(null,"更新成功");
        }
        return Result.error("更新失败");
    }
}
